home *** CD-ROM | disk | FTP | other *** search
- /*
- * Program: DIRSIZE / Author: David Bennett / Date: 6-27-90 / Version: 1.0
- *
- *
- * Usage:
- *
- * DIRSIZE {-n} <directory spec>
- *
- * Where :
- *
- * -n : Specifys no sub-directory recursion. Only the specified
- * directory is shown.
- *
- * Description:
- *
- * This program will display a listing of directory and subdirectories
- * specified in <directory spec>. The listing contains the name of
- * the directory the number of files in that directory and the total
- * size of all files in bytes. A total is printed at the end.
- *
- * Comments & Suggestions:
- *
- * David Bennett
- * Bennett Software Solutions
- * 151 West Geospace Drive
- * Independence, MO 64056
- *
- * CIS: 74635,1671
- * Fido: 1:280/307
- */
-
- #include <alloc.h>
- #include <ctype.h>
- #include <dir.h>
- #include <dos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define MAX_DIR 512 /* Maximum directories supported */
-
- const char *author="(c) David H. Bennett, 1990";
- const char *ver="Version 1.0 - 6/25/90";
-
- /*
- * Directory sizing structure
- */
- typedef struct {
- char *name; /* Name of the directory */
- long size; /* Cummulative size of all files in dir */
- int files; /* Number of files in dir */
- } DOSDIR;
-
- /*
- * Fill the directory array and return the number of diretories in the
- * system. Returns -1 on memory error. Record zero of the array holds
- * the totals of all directories.
- *
- * dname : name of the start directory
- * dn : directory counter, set to zero before calling
- * direc : allocated array of pointers to (unallocated) DOSDIR structs
- * slots : number of pointers allocated in 'direc'
- * nlen : set to 0 before calling, is set to the largest dir name length
- * recurse : If directory recursion is to take place.
- *
- */
- int sizedir(char *dname,int *dn,DOSDIR **direc,int slots,int *nlen,int recurse)
- {
- /*
- * Directory search attriubte
- */
- static const char srchattr = (FA_RDONLY|FA_HIDDEN|FA_SYSTEM| \
- FA_DIREC|FA_ARCH);
-
- char fspec[MAXPATH]; /* Wildcard file spec for search */
- struct ffblk fb; /* used in findfirst/next functions */
- int done; /* end of directory check flag */
- DOSDIR *dir; /* Alias of direc[dn] for code readability */
-
- /*
- * Check to see that number of directories hasn't outgrown our array
- * of pointers.
- */
- if (*dn > slots) return(-1);
-
- /*
- * Check and make sure the array of pointers has been allocated
- */
- if (!direc) return(-1);
-
- /*
- * Allocate the totals directory
- */
- if (*dn == 0) {
- dir = direc[0] = (DOSDIR *) malloc(sizeof(DOSDIR));
- if (!dir) return(-1);
- strcpy(fspec, "Totals");
- dir->name = (char *) malloc(strlen(fspec)+1);
- if (!dir->name) return(-1);
- strcpy(dir->name, fspec);
- direc[0]->files = 0;
- direc[0]->size = 0;
- }
-
- /*
- * Allocate the space for this directory
- */
- (*dn)++; /* Add this directory to the directory counter */
-
- dir = direc[*dn] = (DOSDIR *) malloc(sizeof(DOSDIR));
- if (!dir) return(-1);
- dir->name = (char *) malloc(strlen(dname)+1);
- if (!dir->name) return(-1);
-
- /*
- * Initialize the directory record
- */
- strcpy(dir->name, dname);
- dir->size = 0;
- dir->files = 0;
-
- /*
- * Update maximum directory name length for output
- */
- *nlen = max(*nlen, strlen(dir->name));
-
- /*
- * Add wildcard spec to the directory name
- */
- strcpy(fspec, dir->name);
- strcat(fspec, "*.*"); /* Look for all files */
-
- /*
- * Scan the entire directory structure
- */
- done = findfirst(fspec, &fb, srchattr);
- while (!done) {
- do {
- if (!strcmp(fb.ff_name,".")) break; /* Skip curr/parent specs */
- if (!strcmp(fb.ff_name,"..")) break;
- if ((fb.ff_attrib & FA_DIREC) && recurse) { /* Another dir? */
- strcpy(fspec, dname);
- strcat(fspec, fb.ff_name);
- strcat(fspec, "\\"); /* RECURSE */
- sizedir(fspec, dn, direc, slots, nlen, recurse);
- }
- else { /* File */
- (dir->files)++;
- dir->size += fb.ff_fsize;
-
- (direc[0]->files)++;
- (direc[0]->size) += fb.ff_fsize;
- }
- } while (0);
- done = findnext(&fb);
- }
- return(*dn);
- }
-
- /*
- * main() program entry point
- *
- */
- void main(int argc, char **argv)
- {
- char startdir[MAXPATH]="\\";
- int i, numdirs=0;
- DOSDIR **direc=NULL; /* The directories array */
- int nlen=63; /* Maximum name length */
- int recurse=1; /* Recurse sub-dirs by default */
-
- /*
- * Check command line for starting directory
- */
- for (i=1; i < argc; i++) {
-
- if (argv[i][0] == '-') switch (toupper(argv[i][1])) {
-
- case 'N' :
- recurse = 0;
- break;
-
- /* Add other command line options here */
-
- }
- else { /* Process the directory spec */
- strcpy(startdir, argv[i]);
- strupr(startdir);
- if (startdir[strlen(startdir)-1] != '\\') strcat(startdir, "\\");
- }
- }
-
- /*
- * Allocate the array of pointers
- */
- direc = (DOSDIR **) calloc(MAX_DIR, sizeof(DOSDIR *));
-
- /*
- * Calculate the size of the directorys
- */
- sizedir(startdir, &numdirs, direc, MAX_DIR, &nlen, recurse);
- if (numdirs == -1) {
- fprintf(stderr, "Not enough memory!\n");
- exit(1);
- }
-
- /*
- * Print header
- */
- printf("%-*s %5s %9s\n\n", nlen, "Dir Name", "Files", "Size");
-
- /*
- * Loop for each directory in the array
- */
- for (i=1; i <= numdirs; i++) {
- printf("%-*s %5d %9ld\n", nlen, direc[i]->name, direc[i]->files,\
- direc[i]->size);
- }
-
- /*
- * Print totals
- */
- printf("\nA total of %d directories containing ", numdirs);
- printf("%d files, ", direc[0]->files);
- printf("Total bytes used %ld\n", direc[0]->size);
- }
-